﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace CH.Easy.OpenLogin
{
    public sealed class OpenContext
    {
        private AbstractOpenAPI openAPI;

        /// <summary>
        /// 授权是的构造函数
        /// </summary>
        /// <param name="type"></param>
        public OpenContext(OpenType type)
        {
            switch (type)
            {
                case OpenType.QZone:
                    this.openAPI = new QZoneOpenAPI(new QZoneOauthV2());
                    break;
                case OpenType.TXWeibo:
                    this.openAPI = new TXWeiboOpenAPI(new TXWeiboOauthV2());
                    break;
                case OpenType.SinaWeibo:
                    this.openAPI = new SinaWeiboOpenAPI(new SinaWeiboOauthV2());
                    break;
                default:
                    break;
            }

        }
        /// <summary>
        /// 调用接口函数的构造函数 只定义回调地址
        /// </summary>
        /// <param name="type"></param>
        /// <param name="accessToken"></param>
        /// <param name="openID"></param>
        public OpenContext(OpenType type, string callBack)
        {
            switch (type)
            {
                case OpenType.QZone:
                    this.openAPI = new QZoneOpenAPI(new QZoneOauthV2(callBack));
                    break;
                case OpenType.TXWeibo:
                    this.openAPI = new TXWeiboOpenAPI(new TXWeiboOauthV2(callBack));
                    break;
                case OpenType.SinaWeibo:
                    this.openAPI = new SinaWeiboOpenAPI(new SinaWeiboOauthV2(callBack));
                    break;
                default:
                    break;
            }

        }
        /// <summary>
        /// 调用接口函数的构造函数
        /// </summary>
        /// <param name="type"></param>
        /// <param name="accessToken"></param>
        /// <param name="openID"></param>
        public OpenContext(OpenType type, string accessToken, string openID)
        {
            switch (type)
            {
                case OpenType.QZone:
                    this.openAPI = new QZoneOpenAPI(new QZoneOauthV2(accessToken, openID));
                    break;
                case OpenType.TXWeibo:
                    this.openAPI = new TXWeiboOpenAPI(new TXWeiboOauthV2(accessToken, openID));
                    break;
                case OpenType.SinaWeibo:
                    this.openAPI = new SinaWeiboOpenAPI(new SinaWeiboOauthV2(accessToken, openID));
                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// 调用接口函数的构造函数 只定义回调地址
        /// </summary>
        /// <param name="type"></param>
        /// <param name="accessToken"></param>
        /// <param name="openID"></param>
        public OpenContext(OpenType type, string accessToken, string openID,string callBack)
        {
            switch (type)
            {
                case OpenType.QZone:
                    this.openAPI = new QZoneOpenAPI(new QZoneOauthV2(accessToken, openID, callBack));
                    break;
                case OpenType.TXWeibo:
                    this.openAPI = new TXWeiboOpenAPI(new TXWeiboOauthV2(accessToken, openID, callBack));
                    break;
                case OpenType.SinaWeibo:
                    this.openAPI = new SinaWeiboOpenAPI(new SinaWeiboOauthV2(accessToken, openID, callBack));
                    break;
                default:
                    break;
            }

        }

        public string GetAuthorizeUrl()
        {
            return this.openAPI.GetAuthorizeUrl();
        }

        public AccessTokenDTO GetAccessToken(string code)
        {
            return this.openAPI.GetAccessToken(code);
        }
        public CallBackDTO GetOpenID(string accessToken)
        {
            return this.openAPI.GetOpenID(accessToken);
        }










        public UserDTO GetUserInfo(string format)
        {
            try
            {
                return openAPI.GetUserInfo(format);
            }
            catch (Exception ex)
            {

                return new UserDTO() { is_succeed = false, err = ex.Message };
            }
        }

        public ResultDTO AddWeibo(string format, string content, string clientip, string jing, string wei, string syncflag)
        {
            try
            {
                return openAPI.AddWeibo(format, content, clientip, jing, wei, syncflag);
            }
            catch (Exception ex)
            {

                return new ResultDTO() { is_succeed = false, err = ex.Message };
            }
        }

        public ResultDTO AddPicWeiBo(string format, string content, string clientip, string jing, string wei, string picUrl, string syncflag)
        {
            try
            {
                return openAPI.AddPicWeiBo(format, content, clientip, jing, wei, picUrl, syncflag);
            }
            catch (Exception ex)
            {

                return new ResultDTO() { is_succeed = false, err = ex.Message };
            }
        }
    }
}
